<template>
	<view class="setting">
		<view class="app-logo">
			<image src="../../static/img/indexImg/applogo.png" mode="aspectFill" class='logo-icon'></image>
			<view class="app-tit">{{appName}}</view>
		</view>
		<view class="wrapper">
			<view class="item" @tap="showLang">
				<text>{{i18n.language}}</text>
				<view>
					<text class="active">{{lang}}</text>
					<text class="iconfont">&#xe600;</text>
				</view>
			</view>
			<view class="item" @click="handleClear">
				<text>{{i18n.clear}}</text>
				<view><text class="iconfont">&#xe600;</text></view>
			</view>
		</view>
		<view class="btn-tc" @click="quit">{{i18n.loginOut}}</view>
		<u-popup v-model="show" mode="bottom" border-radius="10">
			<view class="pop-lang">
				<view class="lang-list" v-for="(item,index) in langList" :key="index" @click="setLang(item)">{{item.name}}</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import UpdateVersion from '@/utils/update.js';
	import {
		version,
		appName,
		defaultLanguage
	} from '@/config.js'
	import {
		clearCacheFile
	} from '@/utils/cacheImages.js'
	export default {
		data() {
			return {
				show: false,
				appName,
				version,
				lang: '',
				langList:[
					{name:'中文简体',val:'zh-cn'},
					{name:'English',val:'en-us'}
				]
			};
		},
		onShow() {
			this.onlang()
			uni.setNavigationBarTitle({
				title: this.$t('setting.setting')
			})
		},
		computed: {
			i18n() {
				return this.$t('setting');
			}
		},
		methods: {
			showLang() {
				this.show = true
			},
			onlang() {
				let defaultlang = uni.getStorageSync('lang') || defaultLanguage
				console.log(defaultlang)
				for (let item of this.langList) {
					if (item.val == defaultlang) {
						this.lang = item.name
					}
				}
			},
			setLang(e) {
				this.lang = e.name
				this.$emit('switch', e.val)
				uni.setStorageSync('lang', e.val);
				this.$i18n.locale = uni.getStorageSync('lang');
				uni.setNavigationBarTitle({
					title: this.$t('setting.setting')
				})
				this.show = false
			},
			//清除用户数据除token (以用户ID 结尾的缓存数据)
			clearUserData() {
				let allKey = uni.getStorageInfoSync()
				let uid = uni.getStorageSync('uid')
				let reg = new RegExp(uid + '$')
				if (uid && /^[1-9]\d{4,8}/.test(uid)) {;
					(allKey.keys || []).forEach((key, item) => {
						if (reg.test(key)) {
							uni.removeStorage({
								key
							})
						}
					})
				}
			},
			handleClear() {
				uni.showLoading({
					title: this.$t('setting.clearing')
				});
				this.clearUserData()
				let _this = this
				// #ifdef H5
				setTimeout(() => {
					uni.hideLoading()
					uni.showToast({
						title: this.$t('setting.cleared'),
						icon: 'success',
						duration: 2000
					});
				}, 1000);
				// #endif

				// #ifdef APP-PLUS
				clearCacheFile(res => {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							title:'清除成功',
							icon: 'success',
							duration: 2000
						});
					}, 1000);
				})
				// #endif
			},
			quit() {
				this.$post('api/user/logout').then(res => {
					this.clearUserData()
					uni.removeStorageSync('token');
					this.$toast(res.msg)
					setTimeout(() => {
						uni.reLaunch({
							url: '../login/index'
						})
					}, 1000)
				});
			}
		}
	};
</script>

<style lang="less" scoped>
	.wrapper {
		font-size: 28upx;
	}

	.setting {
		overflow: hidden;

		.modal-body {
			height: 300upx;
			background-color: #fff;
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			border-radius: 20upx 20upx 0upx 0upx;
			text-align: center;

			view {
				line-height: 100upx;
				border-bottom: 1px solid var(--border);

				&:nth-last-child(1) {
					border: none;
				}

				&.cancel {
					color: #9dacb9;
				}
			}
		}

		.wrapper {
			margin: 0 30upx;

			.item {
				display: flex;
				justify-content: space-between;
				line-height: 100upx;
				color: var(--txt-color);
				border-bottom: 1upx solid var(--border);

				.active {
					color: var(--theme-color);
				}

				.iconfont {
					margin-left: 20upx;
					font-size: 24upx;
				}
			}
		}
	}

	.lang-list {
		display: flex;
		align-items: center;
		justify-content: center;
		color: var(--theme-color);
		font-size: 36upx;
		padding: 30upx 0;
		border-top: 1upx solid var(--border);

		&:nth-child(1) {
			border: none;
		}
	}

	.btn-tc {
		width: 690upx;
		height: 100upx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: var(--theme-color);
		font-size: 30upx;
		font-weight: bold;
		color: #ffffff;
		position: absolute;
		bottom: 50upx;
		left: 30upx;
		border-radius: 10upx;
	}
</style>
